package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.ParaMapper;
import com.lemeng.dao.TemplateMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Para;
import com.lemeng.pojo.Template;
import com.lemeng.service.ParaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = ParaService.class)
public class ParaServiceImpl implements ParaService {

    @Autowired
    ParaMapper paraMapper;

    @Override
    public List<Para> findAll() {
        return paraMapper.selectAll();
    }

    @Override
    public PageResult<Para> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Para> paraPageInfo = new PageInfo<>(paraMapper.selectAll());
//        Page<Para> paras = (Page<Para>)paraMapper.selectAll();
        return new PageResult(paraPageInfo.getTotal(),paraPageInfo.getList());
    }

    @Override
    public List<Para> findList(Map<String, Object> serchMap) {
        Example example = new Example(Para.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("templateId")!=null && StringUtil.isNotEmpty(serchMap.get("templateId").toString())){
                criteria.andEqualTo("templateId",serchMap.get("templateId").toString());
            }
        }
        return paraMapper.selectByExample(example);
    }

    @Override
    public PageResult<Para> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Para> paraPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(paraPageInfo.getTotal(),paraPageInfo.getList());
    }

    @Override
    public Para findById(Integer id) {
        return paraMapper.selectByPrimaryKey(id);
    }

    @Autowired
    TemplateMapper templateMapper;

    @Override
    @Transactional
    public void add(Para para) {
        paraMapper.insert(para);

        Template template = templateMapper.selectByPrimaryKey(para.getTemplateId());
        template.setParaNum(template.getParaNum()+1);
        templateMapper.updateByPrimaryKeySelective(template);
    }

    @Override
    public void update(Para para) {
        paraMapper.updateByPrimaryKeySelective(para);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        Para para = paraMapper.selectByPrimaryKey(id);
        paraMapper.deleteByPrimaryKey(id);
        Template template = templateMapper.selectByPrimaryKey(para.getTemplateId());
        template.setParaNum(template.getParaNum()-1);
        templateMapper.updateByPrimaryKeySelective(template);
    }


}
